package com.wisdom.benefit.jufa.base;

import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

/**
 * Created on 2019/08/23 11:31.
 *
 * @author zhoudawei
 */
public class BasePopupWindow extends PopupWindow {

    public BasePopupWindow(View contentView) {
        this(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
    }

    public BasePopupWindow(View contentView, int width, int height, boolean focusable) {
        setContentView(contentView);
        setWidth(width);
        setHeight(height);
        setFocusable(focusable);
        setBackgroundDrawable(new ColorDrawable(Color.parseColor("#88000000")));
        setOutsideTouchable(true);
        setTouchable(true);
        update();
    }

    @Override
    public void showAsDropDown(View anchor, int x_off, int y_off, int gravity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, x_off, y_off, gravity);
    }
}
